<?php

namespace app\common\business\http\wechat;

/**
 * wechat数据来源
 */
class Wechat extends AbstractModel {

    /**
     * 发送红包
     * 参数：weibo数组
     * @return array
     */
    public function getjsapi($url) {
        $url = base64_encode($url);
        $requestUri = '/payment/weixin/getjsapi?backurl=' . $url;

        $data = $this->_request($requestUri, "GET", array(), 30, array(), false);
        return $data;
    }

    /**
     * 获取code
     * 参数：backurl
     * @return array
     */
    public function getcode($backurl) {
        $backurl = base64_encode($backurl);
        
        header('Location: https://xf2.qiu7.net/api/quicklogin/getcode/backurl/' . $backurl . '/');
        exit;
    }

    /**
     * 获取weixin信息
     * 参数：code
     * @return array
     */
    public function getwxuserinfo($code = '', $access_token = '', $openid = '', $unionid = '') {

        $result = $this->_request('/api/quicklogin/getwxuserinfo/?code=' . $code . '&access_token=' . $access_token . '&openid=' . $openid . '&unionid=' . $unionid . '&1=1');
        if (isset($result['unionid']) && !empty($result['unionid'])) {
            setcookie('wxuserinfo', json_encode($result), time() + (60 * 60 * 24), "/", ".qiu7.net");
        } else {
            return false;
        }

        return $result;
    }

    /**
     * 获取weixin信息
     * 参数：code
     * @return array
     */
    public function getauthinfo($code = '', $access_token = '', $openid = '') {

        $result = $this->_request('/api/quicklogin/getauthinfo/?code=' . $code);
        if (isset($result['unionid']) && !empty($result['unionid'])) {
            setcookie('wxauthinfo', json_encode($result), time() + (60 * 60 * 24), "/", ".qiu7.net");
        } else {
            return false;
        }

        return $result;
    }

    /**
     * 保存weixin信息
     * 参数：wxuserinfo数组
     * @return array
     */
    public function savewxinfo($wxuserinfo) {
        $result = $this->_request('/api/quicklogin/savewxinfo/', 'POST', $wxuserinfo);

        return $result;
    }

}
